package com.smartEdu.commons;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

//import ch.qos.logback.classic.LoggerContext;
//import ch.qos.logback.classic.joran.JoranConfigurator;
//import ch.qos.logback.core.joran.spi.JoranException;
//import ch.qos.logback.core.util.StatusPrinter;

//import org.apache.log4j.PropertyConfigurator;
//import org.apache.logging.log4j.core.LoggerContext;
@Component
public class LogCvt {

	private static final Logger logger = LoggerFactory.getLogger("com.smartEdu");

	/*static {
		LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
		JoranConfigurator configurator = new JoranConfigurator();
		configurator.setContext(lc);
		lc.reset();
		try {
			String path = System.getProperty("WORKDIR")+ "config/logback.xml";
			System.err.println(path);
			System.out.println(System.getProperty("WORKDIR"));
//			PropertyConfigurator.configure(path);
			configurator.doConfigure(path);
		} catch (JoranException e) {
			e.printStackTrace();
		}
		StatusPrinter.print(lc.getStatusManager());
	}*/

	public Logger getLogger() {
		return logger;
	}

	// --------------------------------------------LogBack
	// Info----------------------------------

	// Log a message at the INFO level.
	public static void info(String msg) {
		msg = checkMsg(msg);
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(msg.replaceAll("\n", "\r\n"));
//		MDC.put("LINE", (numberOfStr(stringBuffer.toString(), "\n") - 1) + "");
		MDC.put("LINE", Thread.currentThread().getStackTrace()[2].toString());
		logger.info(stringBuffer.toString());
		MDC.remove("LINE");
	}

	public static void info(String msg, String IF) {
		msg = checkMsg(msg);
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(msg.replaceAll("\n", "\r\n"));
//		MDC.put("LINE", (numberOfStr(stringBuffer.toString(), "\n") - 1) + "");
		MDC.put("LINE", Thread.currentThread().getStackTrace()[2].toString());
		MDC.put("IF", IF);
		logger.info(stringBuffer.toString());
		MDC.remove("IF");
		MDC.remove("LINE");
	}

	// Log an exception (throwable) at the INFO level with an accompanying
	// message.
	public static void info(String msg, Throwable t) {
		msg = checkMsg(msg);
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(msg.replaceAll("\n", "\r\n"));
		stringBuffer.append("\r\n");
		stringBuffer.append(getStackTrace(t));
//		MDC.put("LINE", (numberOfStr(stringBuffer.toString(), "\n") - 1) + "");
		MDC.put("LINE", Thread.currentThread().getStackTrace()[2].toString());
		logger.info(stringBuffer.toString());
		MDC.remove("LINE");

	}

	/**
	 * 
	 * @Title: info
	 * @Description: TODO
	 * @author:ailun
	 * @create-date:2012-7-5 ????08:47:18
	 * @modify-date:2012-7-5 ????08:47:18
	 * @param @param msg -->???
	 * @param @param t -->??????
	 * @param @param IF -->?????
	 * @return void
	 * @throws
	 */
	public static void info(String msg, Throwable t, String IF) {
		msg = checkMsg(msg);
		StringBuilder stringBuffer = new StringBuilder();
		stringBuffer.append(msg.replaceAll("\n", "\r\n"));
		stringBuffer.append("\r\n");
		stringBuffer.append(getStackTrace(t));
//		MDC.put("LINE", (numberOfStr(stringBuffer.toString(), "\n") - 1) + "");
		MDC.put("LINE", Thread.currentThread().getStackTrace()[2].toString());
		MDC.put("IF", IF);
		logger.info(stringBuffer.toString());
		MDC.remove("IF");
		MDC.remove("LINE");

	}

	// ---------------------------------------------LogBack
	// Debug------------------------------------

	// Log a message at the DEBUG level.
	public static void debug(String msg) {
		msg = checkMsg(msg);
		StringBuilder stringBuffer = new StringBuilder();
		stringBuffer.append(msg.replaceAll("\n", "\r\n"));
//		MDC.put("LINE", (numberOfStr(stringBuffer.toString(), "\n") - 1) + "");
		MDC.put("LINE", Thread.currentThread().getStackTrace()[2].toString());
		logger.debug(stringBuffer.toString());
		MDC.remove("LINE");
	}

	/**
	 * 
	 * @Title: debug
	 * @Description: TODO
	 * @author:ailun
	 * @create-date:2012-7-5 ????08:48:13
	 * @modify-date:2012-7-5 ????08:48:13
	 * @param @param msg -->???
	 * @param @param IF -->?????
	 * @return void
	 * @throws
	 */
	public static void debug(String msg, String IF) {
		msg = checkMsg(msg);
		StringBuilder stringBuffer = new StringBuilder();
		stringBuffer.append(msg.replaceAll("\n", "\r\n"));
//		MDC.put("LINE", (numberOfStr(stringBuffer.toString(), "\n") - 1) + "");
		MDC.put("LINE", Thread.currentThread().getStackTrace()[2].toString());
		MDC.put("IF", IF);
		logger.debug(stringBuffer.toString());
		MDC.remove("IF");
		MDC.remove("LINE");
	}

	// Log an exception (throwable) at the DEBUG level with an accompanying
	// message.
	public static void debug(String msg, Throwable t) {
		msg = checkMsg(msg);
		StringBuilder stringBuffer = new StringBuilder();
		stringBuffer.append(msg.replaceAll("\n", "\r\n"));
		stringBuffer.append("\r\n");
		stringBuffer.append(getStackTrace(t));
//		MDC.put("LINE", (numberOfStr(stringBuffer.toString(), "\n") - 1) + "");
		MDC.put("LINE", Thread.currentThread().getStackTrace()[2].toString());
		logger.debug(stringBuffer.toString());
		MDC.remove("LINE");

	}

	/**
	 * 
	 * @Title: debug
	 * @Description: TODO
	 * @author:ailun
	 * @create-date:2012-7-5 ????08:48:37
	 * @modify-date:2012-7-5 ????08:48:37
	 * @param @param msg -->???
	 * @param @param t -->?????
	 * @param @param IF -->?????
	 * @return void
	 * @throws
	 */
	public static void debug(String msg, Throwable t, String IF) {
		msg = checkMsg(msg);
		StringBuilder stringBuffer = new StringBuilder();
		stringBuffer.append(msg.replaceAll("\n", "\r\n"));
		stringBuffer.append("\r\n");
		stringBuffer.append(getStackTrace(t));
//		MDC.put("LINE", (numberOfStr(stringBuffer.toString(), "\n") - 1) + "");
		MDC.put("LINE", Thread.currentThread().getStackTrace()[2].toString());
		MDC.put("IF", IF);
		logger.debug(stringBuffer.toString());
		MDC.remove("IF");
		MDC.remove("LINE");

	}

	// -----------------------------LogBack
	// Error-------------------------------------

	// Log a message at the ERROR level.
	public static void error(String msg) {
		msg = checkMsg(msg);
		StringBuilder stringBuffer = new StringBuilder();
		stringBuffer.append(msg.replaceAll("\n", "\r\n"));
//		MDC.put("LINE", (numberOfStr(stringBuffer.toString(), "\n") - 1) + "");
		MDC.put("LINE", Thread.currentThread().getStackTrace()[2].toString());
		logger.error(stringBuffer.toString());
		MDC.remove("LINE");
	}

	/**
	 * 
	 * @Title: error
	 * @Description: TODO
	 * @author:ailun
	 * @create-date:2012-7-5 ????08:49:07
	 * @modify-date:2012-7-5 ????08:49:07
	 * @param @param msg
	 * @param @param IF
	 * @return void
	 * @throws
	 */
	public static void error(String msg, String IF) {
		msg = checkMsg(msg);
		StringBuilder stringBuffer = new StringBuilder();
		stringBuffer.append(msg.replaceAll("\n", "\r\n"));
//		MDC.put("LINE", (numberOfStr(stringBuffer.toString(), "\n") - 1) + "");
		MDC.put("LINE", Thread.currentThread().getStackTrace()[2].toString());
		MDC.put("IF", IF);
		logger.error(stringBuffer.toString());
		MDC.remove("IF");
		MDC.remove("LINE");
	}

	public static void error(String msg, Throwable t) {
		msg = checkMsg(msg);
		StringBuilder stringBuffer = new StringBuilder();
		stringBuffer.append(msg.replaceAll("\n", "\r\n"));
		stringBuffer.append("\r\n");
		stringBuffer.append(getStackTrace(t));
//		MDC.put("LINE", (numberOfStr(stringBuffer.toString(), "\n") - 1) + "");
		MDC.put("LINE", Thread.currentThread().getStackTrace()[2].toString());
		logger.error(stringBuffer.toString());
		MDC.remove("LINE");

	}

	/**
	 * 
	 * @Title: error
	 * @Description: TODO
	 * @author:ailun
	 * @create-date:2012-7-5 ????08:50:05
	 * @modify-date:2012-7-5 ????08:50:05
	 * @param @param msg -->???
	 * @param @param t -->?????
	 * @param @param IF -->?????
	 * @return void
	 * @throws
	 * 
	 */
	public static void error(String msg, Throwable t, String IF) {
		msg = checkMsg(msg);
		StringBuilder stringBuffer = new StringBuilder();
		stringBuffer.append(msg.replaceAll("\n", "\r\n"));
		stringBuffer.append("\r\n");
		stringBuffer.append(getStackTrace(t));
//		MDC.put("LINE", (numberOfStr(stringBuffer.toString(), "\n") - 1) + "");
		MDC.put("LINE", Thread.currentThread().getStackTrace()[2].toString());
		MDC.put("IF", IF);
		logger.error(stringBuffer.toString());
		MDC.remove("IF");
		MDC.remove("LINE");

	}

	// -------------------------LogBack
	// Warn--------------------------------------------
	// Log a message at the TRACE level.
	public static void trace(String msg) {
		msg = checkMsg(msg);
		StringBuilder stringBuffer = new StringBuilder();
		stringBuffer.append(msg.replaceAll("\n", "\r\n"));
//		MDC.put("LINE", (numberOfStr(stringBuffer.toString(), "\n") - 1) + "");
		MDC.put("LINE", Thread.currentThread().getStackTrace()[2].toString());
		logger.trace(stringBuffer.toString());
		MDC.remove("LINE");
	}

	// Log an exception (throwable) at the TRACE level with an accompanying
	// message.
	public static void trace(String msg, Throwable t) {
		msg = checkMsg(msg);
		StringBuilder stringBuffer = new StringBuilder();
		stringBuffer.append(msg.replaceAll("\n", "\r\n"));
		stringBuffer.append("\r\n");
		stringBuffer.append(getStackTrace(t));
//		MDC.put("LINE", (numberOfStr(stringBuffer.toString(), "\n") - 1) + "");
		MDC.put("LINE", Thread.currentThread().getStackTrace()[2].toString());
		logger.trace(stringBuffer.toString());
		MDC.remove("LINE");

	}

	private static int numberOfStr(String str, String con) {
		String str1 = " " + str;
		return str1.split(con).length;

	}

	private static String getStackTrace(Throwable t) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		try {
			t.printStackTrace(pw);
			return sw.toString();
		} finally {
			try {
				pw.close();
				sw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private static String checkMsg(String msg) {
		if (msg == null) {
			msg = "";
		}
		return msg;
	}

	public static void main(String[] args) {

		String s = LogCvt.class.getClass().getResource("/logConfig.xml").getPath();
		System.out.println(s);
	}

}
